// ArchiveName.cpp

#include "StdAfx.h"

#include "Common/FileSystem/FileDir.h"
#include "Common/FileSystem/FileFind.h"

#include "Common/FileSystem/ExtractingFilePath.h"

namespace Common
{
namespace FileSystem
{

static CString CreateArchiveName2(const CString &srcName, bool fromPrev, bool keepName)
{
  CString resultName = _T("Archive");
  if (fromPrev)
  {
    CString dirPrefix;
    if (Common::FileSystem::GetOnlyDirPrefix(srcName, dirPrefix))
    {
      if (dirPrefix.GetLength() > 0)
        if (CHAR_PATH_SEPARATOR == dirPrefix[dirPrefix.GetLength() - 1])
        {
          dirPrefix.Delete(dirPrefix.GetLength() - 1);
          FileSystem::File::CFileInfo fileInfo;
          if (fileInfo.Find(dirPrefix))
            resultName = fileInfo.Name;
        }
    }
  }
  else
  {
    FileSystem::File::CFileInfo fileInfo;
    if (!fileInfo.Find(srcName))
      // return resultName;
      return srcName;
    resultName = fileInfo.Name;
    if (!fileInfo.IsDir() && !keepName)
    {
      int dotPos = resultName.ReverseFind('.');
      if (dotPos > 0)
      {
        CString archiveName2 = resultName.Left(dotPos);
        if (archiveName2.ReverseFind('.') < 0)
          resultName = archiveName2;
      }
    }
  }
  return resultName;
}

CString CreateArchiveName(const CString &srcName, bool fromPrev, bool keepName)
{
  return Common::FileSystem::GetCorrectFsPath(CreateArchiveName2(srcName, fromPrev, keepName));
}

}
}